﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ClassStandardsTableAdapters;


/// <summary>
/// Summary description for ClassStandardsBLL
/// </summary>
[System.ComponentModel.DataObject]
public class ClassStandardsBLL
{
    ILE_Global ileGlobal = new ILE_Global();

    private Courses_StandardTableAdapter _classStandardAdapter = null;
    protected Courses_StandardTableAdapter ClassStandardAdapter
    {
        get
        {
            if (_classStandardAdapter == null)
                _classStandardAdapter = new Courses_StandardTableAdapter();

            return _classStandardAdapter;
        }
    }

    private Courses_Standard_SectionTableAdapter _classStandardSectionAdapter = null;
    protected Courses_Standard_SectionTableAdapter ClassStandardSectionsAdapter
    {
        get
        {
            if (_classStandardSectionAdapter == null)
                _classStandardSectionAdapter = new Courses_Standard_SectionTableAdapter();

            return _classStandardSectionAdapter;
        }
    }

    private Courses_LearnObjTableAdapter _classLearnObjAdapter = null;
    protected Courses_LearnObjTableAdapter ClassLearnObjAdapter
    {
        get
        {
            if (_classLearnObjAdapter == null)
                _classLearnObjAdapter = new Courses_LearnObjTableAdapter();

            return _classLearnObjAdapter;
        }
    }

    private Courses_LearnObj_SectionTableAdapter _classLearnObjSectionAdapter = null;
    protected Courses_LearnObj_SectionTableAdapter ClassLearnObjSectionsAdapter
    {
        get
        {
            if (_classLearnObjSectionAdapter == null)
                _classLearnObjSectionAdapter = new Courses_LearnObj_SectionTableAdapter();

            return _classLearnObjSectionAdapter;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public ClassStandards.Courses_StandardDataTable GetClassStandards()
    {
        return ClassStandardAdapter.GetClassStandards();
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, true)]
    public ClassStandards.Courses_StandardDataTable GetClassStandardsByCourseID(int CourseID)
    {
        return ClassStandardAdapter.GetClassStandardsByCourseID(CourseID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public ClassStandards.Courses_StandardRow GetClassStandard(int CourseID, int StandardID)
    {
        return ClassStandardAdapter.GetClassStandard(CourseID, StandardID)[0];
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, true)]
    public bool InsertClassStandard(int CourseID, int StandardID, bool IsStandardEnabled)
    {
        return ClassStandardAdapter.InsertClassStandard(CourseID, StandardID, "Introduction", "", false, IsStandardEnabled) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, true)]
    public bool UpdateClassStandard(string IntroTitle, string IntroContent, bool IsIntroEnabled, bool IsStandardEnabled, int CourseID, int StandardID)
    {
        return ClassStandardAdapter.UpdateClassStandard(IntroTitle, IntroContent, IsIntroEnabled, IsStandardEnabled, CourseID, StandardID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public bool IsDuplicated(int CourseID, int StandardID)
    {
        return ClassStandardAdapter.IsDuplicated(CourseID, StandardID) != 0;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public ClassStandards.Courses_LearnObjRow GetClassLearnObj(int CourseID, int StandardID, int LearnObjID)
    {
        return ClassLearnObjAdapter.GetClassLearnObj(CourseID, StandardID, LearnObjID)[0];
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public ClassStandards.Courses_LearnObjDataTable GetClassLearnObjs(int CourseID, int StandardID)
    {
        return ClassLearnObjAdapter.GetClassLearnObjsByCourseIdAndStandardId(CourseID, StandardID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public bool InsertClassLearnObj(int CourseID, int StandardID, int LearnObjID, bool UseAlterInfo, string AlterTitle, string AlterDirections, bool IsEnabled)
    {
        int SortOrder = 1;
        ClassStandards.Courses_LearnObjDataTable classLearnObjs = ClassLearnObjAdapter.GetClassLearnObjsByCourseIdAndStandardId(CourseID, StandardID);
        if (classLearnObjs.Count >= 1)
        {
            SortOrder = classLearnObjs[classLearnObjs.Count - 1].SortOrder + 1;
        }
        return ClassLearnObjAdapter.InsertClassLearnObj(CourseID, StandardID, LearnObjID, SortOrder, UseAlterInfo, AlterTitle, AlterDirections, IsEnabled) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateClassLearnObj(bool UseAlterInfo, string AlterTitle, string AlterDirections, bool IsEnabled, int CourseID, int StandardID, int LearnObjID)
    {
        return ClassLearnObjAdapter.UpdateClassLearnObj(UseAlterInfo, AlterTitle, AlterDirections, IsEnabled, CourseID, StandardID, LearnObjID) == 1;
    }

    private bool UpdateClassLearnObjSortOrder(int SortOrder, int CourseID, int StandardID, int LearnObjID)
    {
        return ClassLearnObjAdapter.UpdateClassLearnObjSortOrder(SortOrder, CourseID, StandardID, LearnObjID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool IncreaseClassLearnObjSortOrder(int CourseID, int StandardID, int LearnObjID)
    {
        ClassStandards.Courses_LearnObjDataTable learnObjs = GetClassLearnObjs(CourseID, StandardID);
        ClassStandards.Courses_LearnObjRow learnObj = GetClassLearnObj(CourseID, StandardID, LearnObjID);

        if (learnObj.SortOrder == learnObjs.Count)
        {
            return true;
        }
        else if (learnObj.SortOrder >= 1)
        {
            ClassStandards.Courses_LearnObjRow tempLearnObj = learnObjs[learnObj.SortOrder];
            if (!(ClassLearnObjAdapter.UpdateClassLearnObjSortOrder(learnObj.SortOrder + 1, CourseID, StandardID, LearnObjID) == 1))
                return false;
            if (!(ClassLearnObjAdapter.UpdateClassLearnObjSortOrder(learnObj.SortOrder, tempLearnObj.CourseID, tempLearnObj.StandardID, tempLearnObj.LearnObjID) == 1))
                return false;
            else
                return true;
        }
        else
        {
            return false;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool DecreaseClassLearnObjSortOrder(int CourseID, int StandardID, int LearnObjID)
    {
        ClassStandards.Courses_LearnObjDataTable learnObjs = GetClassLearnObjs(CourseID, StandardID);
        ClassStandards.Courses_LearnObjRow learnObj = GetClassLearnObj(CourseID, StandardID, LearnObjID);

        if (learnObj.SortOrder == 1)
        {
            return true;
        }
        else if (learnObj.SortOrder <= learnObjs.Count)
        {
            ClassStandards.Courses_LearnObjRow tempLearnObj = learnObjs[learnObj.SortOrder - 2];
            if (!(ClassLearnObjAdapter.UpdateClassLearnObjSortOrder(learnObj.SortOrder - 1, CourseID, StandardID, LearnObjID) == 1))
                return false;
            if (!(ClassLearnObjAdapter.UpdateClassLearnObjSortOrder(learnObj.SortOrder, tempLearnObj.CourseID, tempLearnObj.StandardID, tempLearnObj.LearnObjID) == 1))
                return false;
            else
                return true;
        }
        else
        {
            return false;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public bool IsClassLearnObjDuplicated(int CourseID, int StandardID, int LearnObjID)
    {
        return Convert.ToInt32(ClassLearnObjAdapter.IsDuplicated(CourseID, StandardID, LearnObjID)) != 0;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Delete, false)]
    public bool DeleteClassLearnObj(int CourseID, int StandardID, int LearnObjID)
    {
        ClassStandards.Courses_LearnObjDataTable learnObjs = GetClassLearnObjs(CourseID, StandardID);
        if (learnObjs.Count <= 0)
        {
            return false;
        }
        else if (learnObjs.Count == 1)
        {
            return ClassLearnObjAdapter.DeleteClassLearnObj(CourseID, StandardID, LearnObjID) == 1;
        }
        else
        {
            ClassStandards.Courses_LearnObjRow learnObj = GetClassLearnObj(CourseID, StandardID, LearnObjID);

            bool result = ClassLearnObjAdapter.DeleteClassLearnObj(CourseID, StandardID, LearnObjID) == 1;

            for (int i = learnObj.SortOrder; i <= learnObjs.Count - 1; i++)
            {
                ClassLearnObjAdapter.UpdateClassLearnObjSortOrder(learnObjs[i].SortOrder - 1, learnObjs[i].CourseID, learnObjs[i].StandardID, learnObjs[i].LearnObjID);
            }

            return result;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
       (System.ComponentModel.DataObjectMethodType.Select, false)]
    public ClassStandards.Courses_Standard_SectionRow GetClassSectionStandard(int CourseID, int SectionNumber, int StandardID)
    {
        try
        {
            return ClassStandardSectionsAdapter.GetSectionStandard(CourseID, SectionNumber, StandardID)[0];
        }
        catch
        {
            return null;
        }
    }



    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public ClassStandards.Courses_LearnObj_SectionDataTable GetAllSectionLearnObjs()
    {
        return ClassLearnObjSectionsAdapter.GetSectionLearnObjs();
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public ClassStandards.Courses_LearnObj_SectionDataTable GetSectionLearnObjs(int CourseID, int SectionNumber, int StandardID)
    {
        return ClassLearnObjSectionsAdapter.GetSectionLearnObjsByCourseIdAndSectionNumberAndStandardId(CourseID, SectionNumber, StandardID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public ClassStandards.Courses_LearnObj_SectionRow GetSectionLearnObj(int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        return ClassLearnObjSectionsAdapter.GetSectionLearnObj(CourseID, SectionNumber, StandardID, LearnObjID)[0];
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public bool IsSectionLearnObjDuplicated(int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        return Convert.ToInt32(ClassLearnObjSectionsAdapter.IsDuplicated(CourseID, SectionNumber, StandardID, LearnObjID)) != 0;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public bool InsertSectionLearnObj(int CourseID, int SectionNumber, int StandardID, int LearnObjID, bool UseAlterInfo, string AlterTitle, string AlterDirections, bool IsEnabled)
    {
        int SortOrder = 1;
        ClassStandards.Courses_LearnObj_SectionDataTable sectionLearnObjs = GetSectionLearnObjs(CourseID, SectionNumber, StandardID);
        if (sectionLearnObjs.Count >= 1)
        {
            SortOrder = sectionLearnObjs[sectionLearnObjs.Count - 1].SortOrder + 1;
        }
        return ClassLearnObjSectionsAdapter.Insert(CourseID, SectionNumber, StandardID, LearnObjID, SortOrder, UseAlterInfo, AlterTitle, AlterDirections, IsEnabled) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public ClassStandards.Courses_LearnObj_SectionRow GetClassSectionLearnObj(int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        try
        {
            return ClassLearnObjSectionsAdapter.GetSectionLearnObj(CourseID, SectionNumber, StandardID, LearnObjID)[0];
        }
        catch
        {
            return null;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateSectionLearnObj(bool UseAlterInfo, string AlterTitle, string AlterDescription, bool IsEnabled, int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        return ClassLearnObjSectionsAdapter.UpdateSectionLearnObj(UseAlterInfo, AlterTitle, AlterDescription, IsEnabled, CourseID, SectionNumber, StandardID, LearnObjID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public ClassStandards.Courses_LearnObj_SectionDataTable GetClassSectionLearnObjs(int CourseID, int SectionNumber, int StandardID)
    {
        return ClassLearnObjSectionsAdapter.GetSectionLearnObjsByCourseIdAndSectionNumberAndStandardId(CourseID, SectionNumber, StandardID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public bool IsCourseSectionStandardDuplicated(int CourseID, int SectionNumber, int StandardID)
    {
        return ClassStandardSectionsAdapter.IsDuplicated(CourseID, SectionNumber, StandardID) != 0;
    }


    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public bool InsertCourseSectionStandard(int CourseID, int SectionNumber, int StandardID, bool UseSectionInfo, string IntroTitle, string IntroContent, bool IsIntroEnabled, bool IsStandardEnabled)
    {
        return ClassStandardSectionsAdapter.Insert(CourseID, SectionNumber, StandardID, UseSectionInfo, IntroTitle, IntroContent, IsIntroEnabled, IsStandardEnabled) != 0;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateSectionStandard(string IntroTitle, string IntroContent, bool IsIntroEnabled, bool IsStandardEnabled, int CourseID, int SectionNumber, int StandardID)
    {
        return ClassStandardSectionsAdapter.UpdateSectionStandard(IntroTitle, IntroContent, IsIntroEnabled, IsStandardEnabled, CourseID, SectionNumber, StandardID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateUseSectionInfo(bool UseSectionInfo, int CourseID, int SectionNumber)
    {
        return ClassStandardSectionsAdapter.UpdateUseSectionInfoByCourseIdAndSectionNumber(UseSectionInfo, CourseID, SectionNumber) >= 1;
    }

    private bool UpdateClassSectionLearnObjSortOrder(int SortOrder, int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        return ClassLearnObjSectionsAdapter.UpdateSectionLearnObjSortOrder(SortOrder, CourseID, SectionNumber, StandardID, LearnObjID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool IncreaseClassSectionLearnObjSortOrder(int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        ClassStandards.Courses_LearnObj_SectionDataTable learnObjs = GetClassSectionLearnObjs(CourseID, SectionNumber, StandardID);
        ClassStandards.Courses_LearnObj_SectionRow learnObj = GetClassSectionLearnObj(CourseID, SectionNumber, StandardID, LearnObjID);

        if (learnObj.SortOrder == learnObjs.Count)
        {
            return true;
        }
        else if (learnObj.SortOrder >= 1)
        {
            ClassStandards.Courses_LearnObj_SectionRow tempLearnObj = learnObjs[learnObj.SortOrder];

            if (!(ClassLearnObjSectionsAdapter.UpdateSectionLearnObjSortOrder(learnObj.SortOrder + 1, CourseID, SectionNumber, StandardID, LearnObjID) == 1))
                return false;
            if (!(ClassLearnObjSectionsAdapter.UpdateSectionLearnObjSortOrder(learnObj.SortOrder, tempLearnObj.CourseID, tempLearnObj.SectionNumber, tempLearnObj.StandardID, tempLearnObj.LearnObjID) == 1))
                return false;
            else
                return true;
        }
        else
        {
            return false;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool DecreaseClassSectionLearnObjSortOrder(int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        ClassStandards.Courses_LearnObj_SectionDataTable learnObjs = GetClassSectionLearnObjs(CourseID, SectionNumber, StandardID);
        ClassStandards.Courses_LearnObj_SectionRow learnObj = GetClassSectionLearnObj(CourseID, SectionNumber, StandardID, LearnObjID);

        if (learnObj.SortOrder == 1)
        {
            return true;
        }
        else if (learnObj.SortOrder <= learnObjs.Count)
        {
            ClassStandards.Courses_LearnObj_SectionRow tempLearnObj = learnObjs[learnObj.SortOrder - 2];
            if (!(ClassLearnObjSectionsAdapter.UpdateSectionLearnObjSortOrder(learnObj.SortOrder - 1, CourseID, SectionNumber, StandardID, LearnObjID) == 1))
                return false;
            if (!(ClassLearnObjSectionsAdapter.UpdateSectionLearnObjSortOrder(learnObj.SortOrder, tempLearnObj.CourseID, tempLearnObj.SectionNumber, tempLearnObj.StandardID, tempLearnObj.LearnObjID) == 1))
                return false;
            else
                return true;
        }
        else
        {
            return false;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Delete, false)]
    public bool DeleteClassSectionLearnObj(int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        return ClassLearnObjSectionsAdapter.Delete(CourseID, SectionNumber, StandardID, LearnObjID) == 1;
    }
}